/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main;

import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;

/**
 * Fix for default JTextField, also adds the restriction of selecting
 * text when isEditable() is false
 * @author Rik Bauwens
 */
public class JTextField extends javax.swing.JTextField
{
        private Highlighter highlighter = getHighlighter();

        public JTextField()
        {
                super();
        }

        public JTextField(Document doc, String text, int columns)
        {
                super(doc, text, columns);
        }

        public JTextField(int columns)
        {
                super(columns);
        }

        public JTextField(String text)
        {
                super(text);
        }

        public JTextField(String text, int columns)
        {
                super(text, columns);
        }

        //Java bug 6223733 workaround
        @Override
        public void setEditable(boolean b)
        {
                super.setEditable(b);
                SwingUtilities.invokeLater( new Runnable()
                {
                        public void run()
                        {
                                if(isEditable())
                                        setHighlighter(highlighter);
                                else
                                        setHighlighter(null);
                                if(getCaret() != null)
                                        getCaret().setVisible(isEditable());
                        }
                });
        }
}
